/***************************************************************************
 *   Copyright (C) 2009 by Bernhard Neuhofer   *
 *   mail@bneu.at   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

#include "vectorInQueue.h"
#include <iostream>
#include "../tools/log.h"

using namespace std;
void VectorInQueue::fossilCollect ( double upto )
{
  while (_vector.front()->getTimestamp()<upto && _vector.size()>2)
	{
		_vector.erase(_vector.begin());
		_lastRead--;
	}
}
double VectorInQueue::getChannelTime()
{
 if (_lastRead==-1) return -1;
    else return _vector[_lastRead]->getTimestamp();
}
Message* VectorInQueue::getLastRead()
{
 if (_lastRead!=-1)
    return _vector[_lastRead];
 else return NULL;
}
Message* VectorInQueue::getNextUnread()
{
	vector<Message*>::iterator it;
        unsigned int index=-1;
	for ( int index=_lastRead; index<_vector.size()-1;index++ )
	{
	    if (_vector[index+1]->isPositive())
		{
		   _lastRead=index+1;
		   return _vector[index+1];
		}
	}
}

bool VectorInQueue::containsUnread()
{
	FILE_LOG(logDEBUG)<<"VectorInqueue: Size:"<<_vector.size()<<" lastread:"<<_lastRead<<endl;
	FILE_LOG(logDEBUG)<<"VectorInqueue:_vector.size-1:"<<(_vector.size()-1)<<endl;
   for (int index=_lastRead;index<(_vector.size()-1);++index)
	{
	FILE_LOG(logDEBUG)<<"VectorInqueue:here!!!"<<endl;
	  if (_vector[index+1]->isPositive())
		{
		  FILE_LOG(logDEBUG)<<"VectorInQueue: found unread message"<<endl;
		  _lastRead=index+1;
		  return true;
		}
	  else {FILE_LOG(logDEBUG)<<"VectorInqueue: not positive:"<<index<<endl;}
	}
	FILE_LOG(logDEBUG)<<"VectorInQueue: found NO unread message"<<endl;
	return false;
}
void VectorInQueue::setLastRead ( Message* message )
{
  for (int i=0; i<_vector.size()-1;i++)
	{
		if (_vector[i]==message)
		  {
		_lastRead=i;
		break;
		}
	}
}
 ostream& operator<< ( ostream& out,VectorInQueue viq )
{
vector<Message*>::iterator it;
	for ( it=viq._vector.begin();it<viq._vector.end();it++ )
	{
		out<<*it;
	}
	return out;
}
